package top.hcode.hoj.tempjob;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.nutz.lang.Lang;
import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import top.hcode.hoj.common.LazyDao;
import top.hcode.hoj.dao.org.OrgDao;
import top.hcode.hoj.dao.org.OrgUserDao;
import top.hcode.hoj.pojo.entity.org.Org;
import top.hcode.hoj.pojo.entity.org.OrgUser;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

/**
 * 临时任务的触发点
 *
 * @author orangej
 * @since 2025/2/22
 */
@Slf4j
@Profile("!prod")
@RestController
@RequiredArgsConstructor
public class TempTaskHookController {
    private final OrgDao orgDao;
    private final OrgUserDao userDao;
    private final LazyDao dao;

    private final ImportCampusStudentFromExcelService importCampusStudentFromExcelService;

    // 临时任务钩子，为每个校区生成教师账号
    @GetMapping("/temp-task-gen-judge-teacher-for-all-campus")
    @RequiresAuthentication
    public String genTeacherAccountForCampus() {
        // 查询所有校区
        List<Org> campusList = orgDao.queryAllCampus();

        campusList.parallelStream().forEach(campus -> {
            String schoolName = orgDao.getOrgNameById(campus.getParentId());

            for (int i = 0; i < 8; i++) {
                log.info("开始为{} - {}生成教师账号 {}", schoolName, campus.getOrgName(), i);

                String username = Lang.md5(campus.getTreeCode() + i).substring(0, 9);
                OrgUser user = userDao.getByUsernameOrMobile(username, null);
                if (user == null) {
                    user = new OrgUser();
                    user.setUsername(username);
                    user.setRealName(campus.getOrgName() + String.format("%02d", i));
                }

                user.setUserType(1);
                user.setEnabled(true);
                user.setFlag0(false);
                user.setRemark("coll-judge-teacher");
                userDao.saveOrUpdate(user);

                dao.runSql("replace into org_has_user (org_id, user_id, tree_code, user_type, org_type) " +
                           "values (?, ?, ?, ?, ?)", campus.getId(), user.getId(), campus.getTreeCode(), 1, campus.getOrgType());
            }
        });

        return null;
    }

    /**
     * 临时任务钩子，从excel中导入校区学生信息，信息来源：
     * https://docs.qq.com/sheet/DR3pMZnhPUnNZT3pO
     * 需要将该在线表格下载为excel文件后，再通过本接口导入
     *
     * @return
     */
    @GetMapping("/temp-task-import-campus-student-from-excel-0228")
    @RequiresAuthentication
    public String importCampusStudentFromExcel() {
        try {
            File file = new File("d:\\123.xlsx");
            importCampusStudentFromExcelService.importCampusStudentFromExcel(new FileInputStream(file));
            return "ok";
        } catch (Exception e) {
            log.error("error in TempTaskHookController.importCampusStudentFromExcel", e);
            return e.getMessage();
        }
    }

    /**
     * 导入 import_student_0228 表中的学生
     */
    @GetMapping("/temp-task-import-student-0228")
    @RequiresAuthentication
    public String importCampusStudentFromExcel2() {
        try {
            List<String> schoolNameList = dao.queryString("select distinct school_name from import_student_0228");
            importCampusStudentFromExcelService.importStudents(schoolNameList);
            return "ok";
        } catch (Exception e) {
            log.error("error in TempTaskHookController.importCampusStudentFromExcel", e);
            return e.getMessage();
        }
    }


}
